package geometry;
//Zakladamy, ze kolejne segmenty to kolejne boki przeszkody w kierunku anti-clockwise
public class Obstacle {

    Segment[] edges;

    public Obstacle(Segment[] edges) {
        this.edges = edges;
    }

    //podane, poczynajac od najmniejszego wzgledem sortowania katowego
    public Obstacle(Vertex vertices[]) {
        int size = vertices.length;
        edges = new Segment[size];
        for (int i = 0; i < size - 1; i++) {
            edges[i] = new Segment(vertices[i], vertices[i + 1]);
            edges[i].setObstacle(true);

        }
        edges[size - 1] = new Segment(vertices[size - 1], vertices[0]);
    }

    public Segment[] getEdges() {
        return edges;
    }

    public void setEdges(Segment[] edges) {
        this.edges = edges;
    }

    public Segment getEdgeAt(int i) {
        return edges[i];
    }
    
    public void setEdgeAt(int i, Segment s) {
        edges[i] = s;
    }

    public int size() {
        return edges.length;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("OBSTACLE:\n");
        
        for(int i = 0;i<edges.length;i++) {
            sb.append(edges[i].toString());
            sb.append(" ");
        }
        return sb.toString();
    }

    public void moveBy(double dx, double dy) {
        for(int i=0; i!=edges.length; i++)
            edges[i] = new Segment(new Vertex(edges[i].getBegin().getX()+dx,
                                              edges[i].getBegin().getY()+dy),
                                   new Vertex(edges[i].getEnd().getX()+dx,
                                              edges[i].getEnd().getY()+dy));
    }

    public void rotate(double a) {
        a = a*Math.PI*2;
        double sX = edges[0].getBegin().getX();
        double sY = edges[0].getBegin().getY();
        for(int i=0; i!=edges.length; i++) {
            double bX = edges[i].getBegin().getX();
            double bY = edges[i].getBegin().getY();
            double eX = edges[i].getEnd().getX();
            double eY = edges[i].getEnd().getY();
            
            edges[i] = new Segment(new Vertex(sX+(bX-sX)*Math.cos(a) - (bY-sY)*Math.sin(a),
                                              sY+(bX-sX)*Math.sin(a) + (bY-sY)*Math.cos(a)),
                                   new Vertex(sX+(eX-sX)*Math.cos(a) - (eY-sY)*Math.sin(a),
                                              sY+(eX-sX)*Math.sin(a) + (eY-sY)*Math.cos(a)));
                    
        }
    }
}
